<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class SettingController extends AdminbaseController{
	
	protected $options_model;

	public function _initialize() {
		C("SP_ADMIN_DEFAULT_THEME","tciocEx");
		parent::_initialize();
		$this->options_model = D("Common/Options");
	}
	public function ses(){
		$test = new \Common\Lib\TciocSMS;
		$res=$test->sendSMS($mobile="15208320869",$content="",$signStr="",$action="send",$sendTime="");
		var_dump($res);
	}
	
	// 网站信息
	public function site(){
	    C(S('sp_dynamic_config'));//加载动态配置
		$option=$this->options_model->where("option_name='site_options'")->find();
		$cmf_settings=$this->options_model->where("option_name='cmf_settings'")->getField("option_value");
		$tpls=sp_scan_dir(C("SP_TMPL_PATH")."*",GLOB_ONLYDIR);
		$noneed=array(".","..",".svn");
		$tpls=array_diff($tpls, $noneed);
		$this->assign("templates",$tpls);
		
		$adminstyles=sp_scan_dir("public/simpleboot/themes/*",GLOB_ONLYDIR);
		$adminstyles=array_diff($adminstyles, $noneed);
		$this->assign("adminstyles",$adminstyles);
		if($option){
			$this->assign(json_decode($option['option_value'],true));
			$this->assign("option_id",$option['option_id']);
		}
		
		$cdn_settings=sp_get_option('cdn_settings');

		$sms_settings=sp_get_option('sms_settings');

		if(isset($sms_settings['pwd'])){
			$sms_settings['pwd']=tc_pwd_decode($sms_settings['pwd']);
		}

		$this->assign("cdn_settings",$cdn_settings);

		$this->assign("sms_settings",$sms_settings);

		$this->assign("cmf_settings",json_decode($cmf_settings,true));
		
		$this->display();
	}
	
	// 网站信息设置提交
	public function site_post(){
		if (IS_POST) {
			if(isset($_POST['option_id'])){
				$data['option_id']=I('post.option_id',0,'intval');
			}
			$options=I('post.options/a');
			
			$configs["SP_SITE_ADMIN_URL_PASSWORD"]=empty($options['site_admin_url_password'])?"":md5(md5(C("AUTHCODE").$options['site_admin_url_password']));
			$configs["SP_DEFAULT_THEME"]=$options['site_tpl'];
			$configs["DEFAULT_THEME"]=$options['site_tpl'];
			$configs["SP_ADMIN_STYLE"]=$options['site_adminstyle'];
			$configs["URL_MODEL"]=$options['urlmode'];
			$configs["URL_HTML_SUFFIX"]=$options['html_suffix'];
			$configs["COMMENT_NEED_CHECK"]=empty($options['comment_need_check'])?0:1;
			$comment_time_interval=intval($options['comment_time_interval']);
			$configs["COMMENT_TIME_INTERVAL"]=$comment_time_interval;
			$_POST['options']['comment_time_interval']=$comment_time_interval;
			$configs["MOBILE_TPL_ENABLED"]=empty($options['mobile_tpl_enabled'])?0:1;
			$configs["HTML_CACHE_ON"]=empty($options['html_cache_on'])?false:true;
				
			sp_set_dynamic_config($configs);//sae use same function
				
			$data['option_name']="site_options";
			$data['option_value']=json_encode($options);
			if($this->options_model->where("option_name='site_options'")->find()){
				$result=$this->options_model->where("option_name='site_options'")->save($data);
			}else{
				$result=$this->options_model->add($data);
			}
			
			$cmf_settings=I('post.cmf_settings/a');
			$banned_usernames=preg_replace("/[^0-9A-Za-z_\x{4e00}-\x{9fa5}-]/u", ",", $cmf_settings['banned_usernames']);
			$cmf_settings['banned_usernames']=$banned_usernames;

			sp_set_cmf_setting($cmf_settings);
			
			$cdn_settings=I('post.cdn_settings/a');
			sp_set_option('cdn_settings', $cdn_settings);

			$sms_settings=I('post.sms_settings/a');
			if(isset($sms_settings['pwd'])){
				$sms_settings['pwd']=tc_pwd_encode($sms_settings['pwd']);
			}
			if(isset($sms_settings['status'])){
				$sms_settings['status']=1;
			}else{
				$sms_settings['status']=0;
			}
			sp_set_option('sms_settings', $sms_settings);

			if ($result!==false) {
				tc_access_log("网站信息修改保存成功！","admin");
				$this->success("保存成功！");
			} else {
				tc_access_log("网站信息修改保存失败！","admin");
				$this->error("保存失败！");
			}
			
		}
	}
	
	// 密码修改
	public function password(){
		$this->display();
	}
	
	// 密码修改提交
	public function password_post(){
		if (IS_POST) {
			if(empty($_POST['old_password'])){
				$this->error("原始密码不能为空！");
			}
			if(empty($_POST['password'])){
				$this->error("新密码不能为空！");
			}
			$user_obj = D("Common/Users");
			$uid=sp_get_current_admin_id();
			$admin=$user_obj->where(array("id"=>$uid))->find();
			$old_password=I('post.old_password');
			$password=I('post.password');
			if(sp_compare_password($old_password,$admin['user_pass'])){
				if($password==I('post.repassword')){
					if(sp_compare_password($password,$admin['user_pass'])){
						$this->error("新密码不能和原始密码相同！");
					}else{
						$data['user_pass']=sp_password($password);
						$data['id']=$uid;
						$r=$user_obj->save($data);
						if ($r!==false) {
							tc_access_log("网站密码修改成功！","admin");
							$this->success("修改成功！");
						} else {
							tc_access_log("网站密码修改失败！","admin");
							$this->error("修改失败！");
						}
					}
				}else{
					$this->error("密码输入不一致！");
				}
	
			}else{
				$this->error("原始密码不正确！");
			}
		}
	}
	
	// 上传限制设置界面
	public function upload(){
	    $upload_setting=sp_get_upload_setting();
	    $this->assign($upload_setting);
	    $this->display();
	}
	
	// 上传限制设置界面提交
	public function upload_post(){
	    if(IS_POST){
	        $result=sp_set_option('upload_setting',I('post.'));
	        if($result!==false){
				tc_access_log("上传限制设置成功！","admin");
	            $this->success('保存成功！');
	        }else{
				tc_access_log("上传限制设置失败！","admin");
	            $this->error('保存失败！');
	        }
	    }
	    
	}
	
	// 清除缓存
	public function clearcache(){
		sp_clear_cache();
		$this->display();
	}
	public function edit_contact_us_info()
	{
		//实例化变量
		$options = array();
		//初始化模型
		$optionModel = M('options');
		//查询联系我们的信息,并将其显示在 ,这里的查询出来的数据是一维数组
		// 注意:这里做特别说明,因需要判断数据库中是否存在 option_name 为 contact_us 的数据
		// 所以提前去数据库中查询
		$data = $optionModel->where(array('option_name' => 'contact_us'))->find();
		//判断查询到的数据($data)是否为空, $isExist 为bool值, 即 true(数据库中不存在) 或者false(数据库中存在)
		$isExist = empty($data);
		//这里如果是post方式访问,默认为保存数据,get方式访问,则直接显示页面
		if (IS_POST) {
			$article=I("post.post");

			$options = array(//初始化变量

				'web1' => htmlentities($_POST['web1']),//网站1
				'web2' => htmlentities($_POST['web2']),//网站2
				'name' => htmlentities($_POST['name']),//公司名称

				'tel' => htmlentities($_POST['tel']),//电话
				'email' => htmlentities($_POST['email']),//邮箱
				'address' => htmlentities($_POST['address']),//地址

				'weibo' => htmlentities($_POST['weibo']),//微博
				'qq' => htmlentities($_POST['qq']),//qq
				'weixin_img' => htmlentities($_POST['weixin_img']),//微信二维码

				'notice' => htmlentities($_POST['notice']),//重要通知
			);

			//5.判断数据库中是否已经存在 option_name 为 contact_us 的数据
			if ($isExist) {
				//如果数据库中不存在 option_name 为 contact_us 的数据,那么需要添加一条option_name 值为 contact_us 的数据
				$init_data = array(
					'option_name' => 'contact_us',
					'option_value' => json_encode($options),
				);
				//系统默认添加成功,会返回添加后对应的id
				$optionId = $optionModel->data($init_data)->add();
				if ($optionId <= 0) {//添加失败
					$this->error("保存失败!");
				}
				//返回值
				$this->success("保存成功!");
			}

			//在上面添加的逻辑中,执行了就会终止继续执行,所以如果是 数据库中存在 option_name 为 contact_us 的数据
			//则会进入下面的逻辑
			$res = $optionModel->where(array('option_name' => 'contact_us'))->save(array('option_value' => json_encode($options)));
			if ($res === false) {//数据库连接原因,导致保存失败
				$this->error("保存失败!");
			}
			$this->success("保存成功!");
		} else {
			//判断查询到的值是否存在
			if (!$isExist) {
				//取其中需要的值 option_value
				$options = json_decode($data['option_value'], true);
			}
			//将现在变量的值,传递到模板页面
			$this->assign('contact', $options);
			//模板渲染
			$this->display("contact");
		}
	}
}